package com.gxuwz.medical.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
    /**
     * ��ǰʱ��
     * @return
     */
    public static String DateNow(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    //��ȡ��ǰ��
    public static String getNowyear(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return sdf.format(date);
    }

    public static String DatetoS(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    //�ɳ������ڻ������
    public static  int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }

    public static Date StringtoD(String string) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(string);
    }


    public  boolean isInTime(Date submissionDate, Date startTime, Date endTime) {

        if (submissionDate.getTime() == startTime.getTime()

                || submissionDate.getTime() == endTime.getTime()) {

            return true;

        }

        Calendar date = Calendar.getInstance();

        date.setTime(submissionDate);

        Calendar begin = Calendar.getInstance();

        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();

        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {

            return true;

        } else {

            return false;

        }

    }

}
